package handler

import (
	"github.com/gin-gonic/gin"
	"tour-mgmt/model"
)

func TourAdd(c *gin.Context) {
	TourName := c.PostForm("TourName")
	TourAddress := c.PostForm("TourAddress")
	TourScore := c.PostForm("TourScore")
	data := model.Tour{
		TourName:    TourName,
		TourAddress: TourAddress,
		TourScore:   TourScore,
	}
	err := model.DB.Debug().Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func TourDel(c *gin.Context) {
	id := c.Query("id")
	var tour model.Tour
	err := model.DB.Debug().Where("id=?", id).Delete(&tour).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "删除失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "删除成功",
	})
	return
}

func TourList(c *gin.Context) {
	var tour []model.Tour
	err := model.DB.Debug().Find(&tour).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "展示失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "展示成功",
		"data": tour,
	})
	return
}
